/**********************************************************
 * MailServerDelegator.java.java
 * Created by Ashutosh
 * Last modified Date: Sep 18, 2008 10:55:34 AM by Ashutosh
 * Revision: <svn revision #>
 * Version : 0.1
 * Copyright (c) 2008 - 2009 cyberThink Infotech Pvt. Ltd., All rights reserved.
 **********************************************************/

package com.printeverywhere.delegator;

import static com.printeverywhere.util.AttributeConstants.CLOSING_SESSION;
import static com.printeverywhere.util.LoggerConstants.USERACCOUNTSCOUNT;
import static com.printeverywhere.util.LoggerConstants.USERLISTSEARCHCRITERIAEXCEPTION;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.apache.log4j.Logger;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

import com.printeverywhere.dto.Creditcard;
import com.printeverywhere.dto.LocationCategories;
import com.printeverywhere.dto.MailServer;
import com.printeverywhere.dto.MessagingSystemCount;
import com.printeverywhere.dto.User;
import com.printeverywhere.util.HibernateUtil;
import com.printeverywhere.util.SearchCriteria;
import com.printeverywhere.util.SearchListCriteria;

/**
 * The Class MailServerDelegator.
 */
public class MailServerDelegator {

	/** The log. */
	private static Logger log = Logger.getLogger(UserServiceDelegator.class);

	/**
	 * Gets the mail details.
	 *
	 * @param userId the user id
	 *
	 * @return the mail details
	 */
	public static List<MailServer> getMailDetails(int userId) {
		Session session  = HibernateUtil.getSession();
		try {
			Transaction transaction = session.beginTransaction();
			Criteria criteria = session.createCriteria(MailServer.class);
			criteria.add(Restrictions.eq("userByUserId.id", userId));
			List<MailServer> list = criteria.list();
			transaction.commit();
			return list;
		} catch (HibernateException e) {
			log.error("Got exception when getting list for MailServer without search criteria".intern());
			throw e;
		} finally {
			if(log.isInfoEnabled()){
				log.info(CLOSING_SESSION);
			}
			session.flush();
			session.close();
		}
	}

	public static List<MailServer> getConditionMailList(SearchListCriteria searchCriteria) throws HibernateException {
		if (log.isInfoEnabled()) {
			log.info(USERACCOUNTSCOUNT);
		}
		if (searchCriteria == null)
			searchCriteria = new SearchListCriteria();
		Session session = HibernateUtil.getSession();
		Criteria criteria = session.createCriteria(MailServer.class);
		Set<String> keys = searchCriteria.getAttributeNames();
		Iterator<String> it = keys.iterator();
		String key = null;
		Object[] value = null;
		while (it.hasNext()) {
			key = it.next();
			value = searchCriteria.getAttribute(key);
			if (((String) value[1]).equals(SearchListCriteria.EQ)) {
				criteria.add(Restrictions.eq(key, value[0]));
			} else if (((String) value[1]).equals(SearchListCriteria.GE)) {
				criteria.add(Restrictions.ge(key, value[0]));
			} else if (((String) value[1]).equals(SearchListCriteria.GT)) {
				criteria.add(Restrictions.gt(key, value[0]));
			} else if (((String) value[1]).equals(SearchListCriteria.ILIKE)) {
				criteria.add(Restrictions.ilike(key, value[0]));
			} else if (((String) value[1]).equals(SearchListCriteria.ISNOTNULL)) {
				criteria.add(Restrictions.isNotNull(key));
			} else if (((String) value[1]).equals(SearchListCriteria.ISNULL)) {
				criteria.add(Restrictions.isNull(key));
			} else if (((String) value[1]).equals(SearchListCriteria.LE)) {
				criteria.add(Restrictions.le(key, value[0]));
			} else if (((String) value[1]).equals(SearchListCriteria.LIKE)) {
				criteria.add(Restrictions.like(key, value[0]));
			} else if (((String) value[1]).equals(SearchListCriteria.LT)) {
				criteria.add(Restrictions.lt(key, value[0]));
			} else if (((String) value[1]).equals(SearchListCriteria.NE)) {
				criteria.add(Restrictions.ne(key, value[0]));
			} else if (((String) value[1]).equals(SearchListCriteria.IN)) {
				criteria.add(Restrictions.in(key, (Object[]) value[0]));
			}
		}
		Transaction transaction = session.beginTransaction();
		List<MailServer> list;
		try {
			list = criteria.list();
			transaction.commit();
		} catch (HibernateException e) {
			log.error(USERLISTSEARCHCRITERIAEXCEPTION);
			throw e;
		} finally {
			if (log.isInfoEnabled()) {
				log.info(CLOSING_SESSION);
			}
			session.flush();
			HibernateUtil.closeSession();
		}
		if (list.size() > 0)
			return list;
		else
			return null;
		}

	public static MessagingSystemCount getMessageCount(int userId) {
		Session session  = HibernateUtil.getSession();
		try {
			Transaction transaction = session.beginTransaction();
			Criteria criteria = session.createCriteria(MessagingSystemCount.class);
			criteria.add(Restrictions.eq("id", userId));
			List<MessagingSystemCount> list = criteria.list();
			transaction.commit();
			if(list.size() > 0) return list.get(0); else return new MessagingSystemCount();
		} catch (HibernateException e) {
			log.error("Got exception when getting list for MailServer without search criteria".intern());
			throw e;
		} finally {
			if(log.isInfoEnabled()){
				log.info(CLOSING_SESSION);
			}
			session.flush();
			session.close();
		}
	}

	public static MailServer getMailDetail(int messageId) {
		Session session  = HibernateUtil.getSession();
		try {
			Transaction transaction = session.beginTransaction();
			Criteria criteria = session.createCriteria(MailServer.class);
			criteria.add(Restrictions.eq("id", messageId));
			List<MailServer> list = criteria.list();
			transaction.commit();
			if(list.size() > 0) return list.get(0); else return new MailServer();
		} catch (HibernateException e) {
			log.error("Got exception when getting list for MailServer without search criteria".intern());
			throw e;
		} finally {
			if(log.isInfoEnabled()){
				log.info(CLOSING_SESSION);
			}
			session.flush();
			session.close();
		}
	}

	public static void insertMail(MailServer mailServer) throws HibernateException {
		if(log.isInfoEnabled()){
			log.info("Called insertMail method".intern());
		}
		Session session = HibernateUtil.getSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			session.save(mailServer);
			tx.commit();
		} catch (RuntimeException e) {
			log.error("Got exception when insert mailServer".intern());
			if (tx != null)
				tx.rollback();
			throw e;
		} finally {
			if(log.isInfoEnabled()){
				log.info("Closing session".intern());
			}
			session.flush();
			session.close();
		}
	}

	public static void updateMail(MailServer mailServer) throws HibernateException {
		if(log.isInfoEnabled()){
			log.info("Called updateMail method".intern());
		}
		Session session = HibernateUtil.getSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			session.update(mailServer);
			tx.commit();
		} catch (RuntimeException e) {
			log.error("Got exception when update mailServer".intern());
			if (tx != null)
				tx.rollback();
			throw e;
		} finally {
			if(log.isInfoEnabled()){
				log.info("Closing session".intern());
			}
			session.flush();
			session.close();
		}
	}

	public static void deleteMessage(MailServer mailServer) throws HibernateException {
		if(log.isInfoEnabled()){
			log.info("Called deleteMessage method".intern());
		}
		Session session = HibernateUtil.getSession();
		Transaction transaction = null;
		try {
			transaction = session.beginTransaction();
			session.refresh(mailServer);
			session.delete(mailServer);
			transaction.commit();
		} catch (RuntimeException e) {
			log.error("Got exception when Delete Message".intern());
			if (transaction != null)
				transaction.rollback();
			throw e;
		} finally {
			if(log.isInfoEnabled()){
				log.info(CLOSING_SESSION);
			}
			session.flush();
			session.close();
		}
	}

	/**
	 * Gets the outbox mail.
	 *
	 * @param userId the user id
	 *
	 * @return the outbox mail
	 */
	public static List<MailServer> getOutboxMail(String userId) {
		Session session  = HibernateUtil.getSession();
		try {

		} catch (Exception e) {
			// TODO: handle exception
		}
		return null;
	}
}